require('../css/commCss/normalize.css')
require('../css/commCss/reset.css')
require('../css/commCss/property.css')
require('../css/commCss/basic.css')

require('../lib/iconfont/iconfont.css')

const util=require('../lib/util/util')
util.runCommonHeader('short-part')
util.commonFoot('run')

// axios
const axios=require('axios')

require('../css/short-part.less')

window.addEventListener('DOMContentLoaded',function(){
    let newClass=document.querySelector('#newClass')
    let anpartList=document.querySelector('#anpartList')
    // console.log(anpartList);

    axios.get('http://139.9.177.51:8099/sports/allcourse')
    .then(function(res){
        // console.log(res.data.data);
        let data=res.data.data
        // 新课程
        let newCourse=[]
        // 旧课程
        let oldCourse=[]
        newCourse=data.filter(function(item){
            return item.latest===1
        })
        oldCourse=data.filter(function(item){
            return item.latest===0
        })
        console.log(newCourse);
        console.log(oldCourse);

        // 新课程的单渲染
        document.querySelector('#newpartImg').src="http://139.9.177.51:8099"+newCourse[0].imgurl
        document.querySelector('#newpartTip').textContent=newCourse[0].name
        document.querySelector('#newpartdesc').textContent=newCourse[0].desc

        // 旧课程的多渲染
        document.querySelector('#anpartA').innerHTML=oldCourse.map(function(item){
            return `
               <li data-cid="${item.courseId}">
                  <img src="${'http://139.9.177.51:8099'+item.imgurl}" alt="" />
                  <div>
                    <p class="f16 fwb mb8">${item.name}</p>
                    <p>${item.desc}</p>
                  </div>
                </li>
            `
        }).join("")

        newClass.addEventListener('click',function(){
            // 获取cid
            newClass.dataset.cid=newCourse[0].courseId
            // 存储到会话存储
            sessionStorage.setItem('cid',this.dataset['cid'])
        })

        anpartList.addEventListener('click',function(e){
            // 获取
            let cid=e.target.closest('li').dataset['cid']
            // 存储
            sessionStorage.setItem('cid',cid)
        })
    })
})